use strict;
use warnings;
use Data::Dumper;
use Benchmark qw( cmpthese ) ;

my @masas_aas;
use Core::IntConsts qw(get_masas_aa get_masas_mod);
my %masas_aa = %{&get_masas_aa};
my %masas_mod = %{&get_masas_mod};


my $z = 2;
my $limit_mz_sup = 4000;
my $limit_mz_inf = 100;

#Ascore 548
sub prever_fragmentos{
	#Predict the spectrum of a sequence. Allows phosphorilations on
	# STY (+80:s,t,y), water loss on ST (-18:u,v), and Met oxidation (+18:m).
	#It calculates the y and b series for the charge state,
	#ARGUMENTS:
	#     - $_[0]-> sequence
	#     - $_[1]-> charge state (1,2 or 3)
	#     - $_[2]-> mz upper limit for fragment generation
	#RETURN: a hash with {label}->mass.
	my $peptide = "L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR";
	#my $peptide = $_[0];
	#my $z = $_[1];
	#my $limit_mz_sup = $_[2];
	#my $limit_mz_inf = $_[3];
	my (@aas, @mods);
	my @masas_aas;

	while($peptide =~ /\w/){
		my $aa;
		my $mod;
		if($peptide =~ s/^(\w)\((\w+\,\w+)\)//){
			$aa = $1;
			$mod = $2;
		}
		elsif($peptide =~ s/^(\w)\((\w+)\)//){
			$aa = $1;
			$mod = $2;
		}
		else{
			$peptide =~ s/^(\w)//;
			$aa = $1;
			$mod = '';
		}
		push @aas, $aa;
		push @mods, $mod;
	}

    for(0..$#aas){
		if($mods[$_]){
			if($mods[$_] =~ /\,/){
				my ($m1, $m2) = split /\,/, $mods[$_];
				$masas_aas[$_] = $masas_mod{$m1} + $masas_mod{$m2} + $masas_aa{$aas[$_]};
			}
			else{
				$masas_aas[$_] = $masas_mod{$mods[$_]} + $masas_aa{$aas[$_]};
			}
		}
		else{
			$masas_aas[$_] = $masas_aa{$aas[$_]};
		}
	}
#sigue..................
	return \@masas_aas;
}


sub prever_fragmentos_2{
	#Predict the spectrum of a sequence. Allows phosphorilations on
	# STY (+80:s,t,y), water loss on ST (-18:u,v), and Met oxidation (+18:m).
	#It calculates the y and b series for the charge state,
	#ARGUMENTS:
	#     - $_[0]-> sequence
	#     - $_[1]-> charge state (1,2 or 3)
	#     - $_[2]-> mz upper limit for fragment generation
	#RETURN: a hash with {label}->mass.
	my $peptide = "L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR";
	#my $peptide = $_[0];
	#my $z = $_[1];
	#my $limit_mz_sup = $_[2];
	#my $limit_mz_inf = $_[3];
	my @masas_aas;

	while($peptide){
		if($peptide =~ s/^(\w)\((\w+),(\w+)\)//){
			push @masas_aas, ($masas_aa{$1} + $masas_mod{$2} + $masas_mod{$3});
		}
		elsif($peptide =~ s/^(\w)\((\w+)\)//){
			push @masas_aas, ($masas_aa{$1} + $masas_mod{$2});
		}
		else{
			$peptide =~ s/^(\w)//;
			push @masas_aas, ($masas_aa{$1});
		}
	}
	return \@masas_aas;
}


#my $pep = "L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR";
#my $z = 1;


#"L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR" => '2892.32435',
print Dumper prever_fragmentos('L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR', 1);
print Dumper prever_fragmentos_2('L(214)QDSSDPDTGSEEEGS(23)SRLSPPHSPR', 1);
##
#__END__

cmpthese(100000, {
        a => \&prever_fragmentos,
        b => \&prever_fragmentos_2,
        #c => \&pep,
        #d => \&pep
        } );



1;
